package com.zx.deploy.controller;

import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zx.deploy.common.annotation.NotAuthPassport;
import com.zx.deploy.common.utils.CommonUtil;
import com.zx.deploy.common.utils.ReturnModel;
import com.zx.deploy.model.Users;
import com.zx.deploy.service.UserService;

/**
 * 登录控制器
 * 
 * @author jbyu
 *
 */
@Controller
public class LoginController {
	
	protected static Logger log = LogManager.getLogger(CommonUtil.class);
	@Autowired
	protected UserService userService;
	
	/**
	 * 登录页面
	 * @return
	 */
	@GetMapping("/login")
	@NotAuthPassport
	public String login() {

		return "login";
	}
	
	/**
	 * 登出
	 * @return
	 */
	@GetMapping("/logout")
	@NotAuthPassport
	public String logout(HttpSession session) {
		session.invalidate();
		return login();
	}

	/**
	 * 登录方法
	 * @return
	 */
	@PostMapping("/dologin")
	@NotAuthPassport
	@ResponseBody
	public Object dologin(HttpSession session,String psd,String loginid) {
		ReturnModel rm = new ReturnModel();
		try {
			Users user = userService.login(psd,loginid);
			session.setAttribute("userLogin", user);
			rm.setInfo(true, "登录成功...正在进行跳转");
		} catch (Exception ex) {
			CommonUtil.exceptionProcess(log, ex, rm, "登录异常");
		}
		return rm;
	}
}
